package com.xinghai.web.controller.dingtalk;

import com.xinghai.common.annotation.Anonymous;
import com.xinghai.common.annotation.Log;
import com.xinghai.common.core.controller.BaseController;
import com.xinghai.common.core.domain.AjaxResult;
import com.xinghai.common.core.page.TableDataInfo;
import com.xinghai.common.enums.BusinessType;
import com.xinghai.common.exception.ServiceException;
import com.xinghai.common.utils.StringUtils;
import com.xinghai.common.utils.poi.ExcelUtil;
import com.xinghai.system.domain.XhUseVehicleInfoDetails;
import com.xinghai.system.service.DingdingOaService;
import com.xinghai.system.service.IXhUseVehicleInfoDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 用车信息评论关联Controller
 * 
 * @author xinghai
 * @date 2024-07-17
 */
@RestController
@RequestMapping("/system/details")
public class XhUseVehicleInfoDetailsController extends BaseController
{
    @Autowired
    private IXhUseVehicleInfoDetailsService xhUseVehicleInfoDetailsService;
    @Autowired
    private DingdingOaService dingdingOaService;

    /**
     * 查询用车信息评论关联列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(XhUseVehicleInfoDetails xhUseVehicleInfoDetails)
    {
        startPage();
        List<XhUseVehicleInfoDetails> list = xhUseVehicleInfoDetailsService.list(xhUseVehicleInfoDetails);
        return getDataTable(list);
    }

    /**
     * 导出用车信息评论关联列表
     */
    @Log(title = "用车信息评论关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XhUseVehicleInfoDetails xhUseVehicleInfoDetails)
    {
        List<XhUseVehicleInfoDetails> list = xhUseVehicleInfoDetailsService.list(xhUseVehicleInfoDetails);
        ExcelUtil<XhUseVehicleInfoDetails> util = new ExcelUtil<XhUseVehicleInfoDetails>(XhUseVehicleInfoDetails.class);
        util.exportExcel(response, list, "用车信息评论关联数据");
    }

    /**
     * 获取用车信息评论关联详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:details:query')")
    @Anonymous
    @GetMapping("/getInfo")
    public AjaxResult getInfo(Long id)
    {
        return success(xhUseVehicleInfoDetailsService.getInfo(id));
    }

    /**
     * 新增用车信息评论关联
     */
//    @PreAuthorize("@ss.hasPermi('system:details:add')")
//    @Log(title = "用车信息评论关联", businessType = BusinessType.INSERT)
    @Anonymous
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody XhUseVehicleInfoDetails xhUseVehicleInfoDetails)
    {
        String str = xhUseVehicleInfoDetails.getImgLinks();
        if(StringUtils.isNotBlank(xhUseVehicleInfoDetails.getImgLinks())){
            String substring = xhUseVehicleInfoDetails.getImgLinks().substring(str.indexOf("/", str.indexOf("/", str.indexOf("/") + 1) + 1) + 1);
            xhUseVehicleInfoDetails.setDingtalkImgLinks(xhUseVehicleInfoDetails.getImgLinks());
            xhUseVehicleInfoDetails.setImgLinks(substring);
        }
        int insert = xhUseVehicleInfoDetailsService.insert(xhUseVehicleInfoDetails);
        boolean b = insert > 0;
        if(b){
            try{
                dingdingOaService.addComments(xhUseVehicleInfoDetails.getInfoId());
            }catch (Exception e){
               e.printStackTrace();
            }
        }
        return toAjax(b);
    }

    /**
     * 修改用车信息评论关联
     */
    @Log(title = "用车信息评论关联", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody XhUseVehicleInfoDetails xhUseVehicleInfoDetails)
    {
        return toAjax(xhUseVehicleInfoDetailsService.update(xhUseVehicleInfoDetails));
    }

    /**
     * 删除用车信息评论关联
     */
    @Log(title = "用车信息评论关联", businessType = BusinessType.DELETE)
	@PostMapping("/delete")
    public AjaxResult delete(Long id)
    {
        return toAjax(xhUseVehicleInfoDetailsService.delete(id));
    }


    /**
     * 批量删除用车信息评论关联
     */
    @Log(title = "用车信息评论关联", businessType = BusinessType.DELETE)
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(Long[] ids)
    {
        return toAjax(xhUseVehicleInfoDetailsService.deleteBatch(ids));
    }

    /**
     * 导入用车信息评论关联模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<XhUseVehicleInfoDetails> util = new ExcelUtil<XhUseVehicleInfoDetails>(XhUseVehicleInfoDetails.class);
        util.importTemplateExcel(response, "用车信息评论关联数据模板");
    }


    /**
     * 导入用车信息评论关联
     */
    @Log(title = "用车信息评论关联管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        ExcelUtil<XhUseVehicleInfoDetails> util = new ExcelUtil<XhUseVehicleInfoDetails>(XhUseVehicleInfoDetails.class);
        List<XhUseVehicleInfoDetails> xhUseVehicleInfoDetailsList = null;
        try {
            xhUseVehicleInfoDetailsList = util.importExcel(file.getInputStream());
        } catch (IOException e) {
            throw new ServiceException("导入数据失败");
        }
        return success(xhUseVehicleInfoDetailsService.importData(xhUseVehicleInfoDetailsList));
    }
}
